#include "wopDrawing2D.h"
#include "wopColorRGBA.h"
#include "wopVector3f.h"
#include "wopTextureManager.h"
#include "wopTexture2D.h"
#include "wopImageQImage.h"

#include <GL/glut.h>

wopTexture2D* mTexture;
GLuint _texID;

void init()
{
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glShadeModel(GL_SMOOTH);

	glHint(GL_LINE_SMOOTH_HINT, GL_FASTEST);
	glEnable(GL_LINE_SMOOTH);

	wopImageQImage _image;
	_image.loadImage(".\\Resources\\Textures\\wall.png");

	wopTextureManager* _textureManager = wopTextureManager::getTextureManager();
	mTexture = _textureManager->createTexture2D(_image);

	glFlush();
}

void display()
{
	glLoadIdentity();
	glTranslatef( 0.f, 0.f, -6.f);

	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

	glPushMatrix();

	glTranslatef( -1.f, -1.f, 0.f);

	// Draw a textured rectangle
	{
		glEnable(GL_TEXTURE_2D);
		mTexture->bind();

		glColor4f(1.0, 1.0, 1.0, 1.0);
		wopDrawing2D::drawRectangleTextured(3.f, 2.f);

		mTexture->unbind();
		glDisable(GL_TEXTURE_2D);
	}

	// Draw a blue triangle
	wopColorRGBA _blue(0.0f, 0.0f, 1.0f, 1.0f);
	glTranslatef(1.0f, 2.0f, 0.0f);
	wopDrawing2D::drawTriangle(wopVector3f(0.0f, 0.0f, 0.0f),
		wopVector3f(2.0f, 0.0f, 0.0f), wopVector3f(1.0f, 2.0f, 0.0f), _blue); 

	// Draw a yellow circle
	glTranslatef(-1.f, -3.0f, 0.0f);
	wopColorRGBA _yellow(1.0, 1.0f, 0.0f, 1.0f);
	wopDrawing2D::drawCircle(1.0f, _yellow);

	glPopMatrix();

	glFlush();
}

void render()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glClearColor(0.0, 0.0, 0.0, 1.0);
    
	display();

	glutSwapBuffers();
	glutPostRedisplay();
}

void reshape(int w, int h)
{
	glViewport(0, 0, (GLsizei)w, (GLsizei)h);
	glMatrixMode( GL_PROJECTION );
	glLoadIdentity();
	//glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0);
	glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
	glMatrixMode( GL_MODELVIEW );
	glLoadIdentity();
}

void mouse(int button, int state, int x, int y)
{
}

int main(int argc, char** argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
  glutInitWindowSize(600, 600);
	glutInitWindowPosition(100, 100);
  glutCreateWindow("World Of Pixels");

	init();

  glutDisplayFunc(&render);
	glutReshapeFunc(&reshape);
	glutMouseFunc(mouse);

  glutMainLoop();
  return 0;
}